package com.CST.service.impl.cb;

import cn.hutool.core.date.DateUtil;
import com.CST.common.api.Response;
import com.CST.common.constant.Constants;
import com.CST.entity.cb.CusDutyfactor;
import com.CST.entity.cb.selectInfo;
import com.CST.mapper.cst.cb.CusDutyfactorMapper;
import com.CST.service.cb.ICusDutyfactorService;
import com.CST.util.HttpUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author ly
 * @since 2021-12-22
 */
@Service
public class CusDutyfactorServiceImpl extends ServiceImpl<CusDutyfactorMapper, CusDutyfactor> implements ICusDutyfactorService {

    @Autowired
    private ICusDutyfactorService cusDutyfactorService;

    @Autowired
    private CusDutyfactorMapper cusDutyfactorMapper;


    @Override
    public Response addRoleworkfactor(CusDutyfactor entity) {
        try {
            Date date = DateUtil.date();
            int month = DateUtil.month(date);
            int year = DateUtil.year(date);
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            entity.setCreatedon(sdf.format(currDate));
            entity.setUpdatedon(sdf.format(currDate));
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            entity.setCreatedby(userName);
            entity.setUpdatedby(userName);
            entity.setYear(String.valueOf(year));
            entity.setMonth(String.valueOf(month +1));
            cusDutyfactorMapper.addRoleworkfactor(entity);
        } catch (Exception ex) {
            return Response.error(String.join(":", "添加失败:", ex.getMessage()));
        }
        return Response.success("添加成功");
    }

    @Override
    public List<CusDutyfactor> selectworkfactor(CusDutyfactor entity) {
        return cusDutyfactorMapper.selectworkfactor(entity);
    }

    @Override
    public List<selectInfo> getYearMonth() {
        return cusDutyfactorMapper.getYearMonth();
    }


    @Override
    public Response Deleteinformation(List<Integer> ids) {
        try {
            cusDutyfactorMapper.Deleteinformation(ids);
        } catch (Exception ex) {
            return Response.error("删除失败");
        }
        return Response.success("删除成功");
    }

    @Override
    public Response modifyworkfactor(CusDutyfactor entity) {
        if (entity.getId() == 0) {
            return Response.error("主键不能为空");
        }
        try {
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:hh:ss");
            entity.setCreatedon(sdf.format(currDate));
            entity.setUpdatedon(sdf.format(currDate));
            String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
            userName = userName == null || userName.isEmpty() ? "System" : userName;
            entity.setCreatedby(userName);
            entity.setUpdatedby(userName);
            cusDutyfactorMapper.modifyworkfactor(entity);
        } catch (Exception ex) {
            return Response.error(String.join(":", "修改失败", ex.getMessage()));
        }
        return Response.success("修改成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Response insertRoleworkfactor(CusDutyfactor entity) {
        try {
            CusDutyfactor searchInfo = new CusDutyfactor();
            searchInfo.setYearMonth(entity.getYearMonth());
            List<CusDutyfactor> sourceList = selectworkfactor(searchInfo);
            if (CollectionUtils.isEmpty(sourceList)) {
                return Response.error("继承失败,没有找到对应规则下的数据");
            }

            Calendar cal = Calendar.getInstance();
            Date currDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:hh:ss");
//            当前规则年月
            String currRule = String.join("-", String.valueOf(cal.get(Calendar.YEAR)), String.valueOf(cal.get(Calendar.MONTH) + 1));
            //清除当前月记录
            cusDutyfactorMapper.deleteByYearMonth(currRule);
            int total = sourceList.size();//为防止插入字符超过最大数,分批插入
            int fromIndex = 0;
            List<CusDutyfactor> insertList = new ArrayList<>();
            for (int i = 0; i < sourceList.size(); i++) {
                CusDutyfactor item = sourceList.get(i);
                item.setYear(String.valueOf(cal.get(Calendar.YEAR)));
                item.setMonth(String.valueOf(cal.get(Calendar.MONTH) + 1));
                item.setCreatedon(sdf.format(currDate));
                item.setUpdatedon(sdf.format(currDate));
                String userName = HttpUtil.getRequestHeaderInfo(Constants.USER_NAME);
                userName = userName == null || userName.isEmpty() ? "System" : userName;
                item.setCreatedby(userName);
                item.setUpdatedby(userName);
                fromIndex++;
                insertList.add(item);
                if (fromIndex % 20 == 0 && fromIndex <= total) {
                    cusDutyfactorMapper.insertRoleworkfactor(insertList);
                    insertList = new ArrayList<>();
                }
            }
            if (CollectionUtils.isNotEmpty(insertList)) {
                cusDutyfactorMapper.insertRoleworkfactor(insertList);
            }

        } catch (Exception ex) {
            return Response.error(String.join(":", "继承失败:", ex.getMessage()));
        }
        return Response.success("继承成功");
    }

}
